<!---
Copyright: (c) 2006 The MachBlog Authors
Authors: Matt Woodward (mpwoodward@gmail.com) & Peter J. Farrell (pjf@maestropublishing.com)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. 
You may obtain a copy of the License at 

http://www.apache.org/licenses/LICENSE-2.0 

Unless required by applicable law or agreed to in writing, software 
distributed under the License is distributed on an "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
See the License for the specific language governing permissions and 
limitations under the License.

$Id: TrackbackListener.cfc 940 2006-08-24 01:09:59Z mpwoodward $

Notes:
This listener is autowired by ColdSpring.
--->
<cfcomponent
	displayname="TrackbackListener"
	extends="MachII.framework.Listener"
	output="false"
	hint="TrackbackListener for MachBlog.">

	<!---
	INITIALIZATION / CONFIGURATION
	--->
	<cffunction name="configure" access="public" output="false" returntype="void" 
		hint="Configures this listener as part of the Mach-II framework.">
		<!--- Does nothing. --->
	</cffunction>

	<cffunction name="setTrackbackService" access="public" output="false" returntype="void"
		hint="Dependency: injected">
		<cfargument name="trackbackService" type="org.machblog.trackback.TrackbackService" required="true" />
		<cfset variables.trackbackService = arguments.trackbackService />
	</cffunction>	
	<cffunction name="getTrackbackService" access="private" output="false" returntype="org.machblog.trackback.TrackbackService">
		<cfreturn variables.trackbackService />
	</cffunction>
	
	<!---
	PUBLIC FUNCTIONS - GATEWAY RELATED
	--->
	<cffunction name="getTrackbacks" access="public" output="false" returntype="query" 
		hint="Returns a query object containing trackbacks for an entry.">
		<cfargument name="event" type="MachII.framework.Event" required="true" />
		<cfreturn getTrackbackService().getTrackbacks(arguments.event.getArg("entryId")) />
	</cffunction>
	
	<cffunction name="getRecentTrackbacks" access="public" output="false" returntype="query" 
		hint="Returns a query object containing recent trackbacks across all entries.">
		<cfargument name="event" type="MachII.framework.Event" required="true" />
		
		<cfset var numToGet = getProperty("numRecentTrackbacksToDisplay") />
		<cfset var includeInactive = true />
		
		<cfif arguments.event.isArgDefined("numTrackbacksToGet")>
			<cfset numToGet = arguments.event.getArg("numTrackbacksToGet") />
		</cfif>
		
		<cfif arguments.event.isArgDefined("includeInactiveTrackbacks")>
			<cfset includeInactive = arguments.event.getArg("includeInactiveTrackbacks") />
		</cfif>
		
		<cfreturn getTrackbackService().getRecentTrackbacks(numToGet, includeInactive) />
	</cffunction>
	
	<cffunction name="getTodaysTrackbackCount" access="public" output="false" returntype="numeric" 
			hint="Returns the number of trackbacks added on the current date">
		<cfreturn getTrackbackService().getTrackbackCountByDate() />
	</cffunction>
	
	<cffunction name="deactivateTrackbacksByEntryId" access="public" output="false" returntype="void" 
			hint="Deactivates trackbacks for an entry">
		<cfargument name="event" type="MachII.framework.Event" required="true" />
		<cfset getTrackbackService().deactivateTrackbacksByEntryId(arguments.event.getArg("entryId")) />
	</cffunction>
	
	<cffunction name="activateTrackbacksByEntryId" access="public" output="false" returntype="void" 
			hint="Activates trackbacks for an entry">
		<cfargument name="event" type="MachII.framework.Event" required="true" />
		<cfset getTrackbackService().activateTrackbacksByEntryId(arguments.event.getArg("entryId")) />
	</cffunction>
	
	<cffunction name="deleteTrackbacksByEntryId" access="public" output="false" returntype="void" 
			hint="Deletes trackbacks for an entry">
		<cfargument name="event" type="MachII.framework.Event" required="true" />
		<cfset getTrackbackService().deleteTrackbacksByEntryId(arguments.event.getArg("entryId")) />
	</cffunction>
	
	<cffunction name="toggleTrackbackDisplay" access="public" output="false" returntype="void" 
		hint="Approves a trackback and announces the next event.">
		<cfargument name="event" type="MachII.framework.Event" required="true" />
		
		<cfset var exitEvent = arguments.event.getArg("exitEvent", "showTrackbackAdmin") />
		
		<cfset getTrackbackService().toggleTrackbackDisplay(arguments.event.getArg("trackbackId"), 
																	arguments.event.getArg("action")) />
		
		<cfset announceEvent(exitEvent, arguments.event.getArgs()) />
	</cffunction>
	
	<cffunction name="deleteTrackbacks" access="public" output="false" returntype="void" 
		hint="Deletes multiple trackbacks using the ids passed in">
		<cfargument name="event" type="MachII.framework.Event" required="true" />
		
		<cfset var exitEvent = arguments.event.getArg("exitEvent", "showTrackbackAdmin") />
		<cfset var results = StructNew() />
		<cfset results.success = true />
		<cfset results.message = getProperty("resourceBundleService").getResourceBundle().getResource("trackbackdeletesuccess") />
		
		<cftry>
			<cfset getTrackbackService().deleteTrackbacks(arguments.event.getArg("deleteIds")) />
			<cfcatch type="any">
				<cfset results.success = false />
				<cfset results.message = getProperty("resourceBundleService").getResourceBundle().getResource("trackbackdeletefailed") />
			</cfcatch>
		</cftry>
		
		<cfset arguments.event.setArg("results", results) />
		<cfset announceEvent(exitEvent, arguments.event.getArgs()) />
	</cffunction>
	
	<!---
	PUBLIC FUNCTIONS - DAO RELATED
	--->
	<cffunction name="getTrackback" access="public" output="false" returntype="org.machblog.trackback.Trackback"
		hint="Retrieves a trackback by ID.">
		<cfargument name="event" type="MachII.framework.Event" required="true" />
		
		<cfreturn getTrackbackService().getTrackbackById(arguments.event.getArg("trackbackId", "")) />
	</cffunction>
	
	<cffunction name="processTrackbackForm" access="public" output="false" returntype="void"
		hint="Processes the trackback form and announces the next event.">
		<cfargument name="event" type="MachII.framework.Event" required="true" />
		
		<cfset var trackback = arguments.event.getArg("trackback") />
		<cfset var results = StructNew() />

		<cfset results.success = true />
		<cfif getProperty("moderateTrackbacks")>
			<cfset results.message = getProperty("resourceBundleService").getResourceBundle().getResource("trackbackmoderationmessage") />
		<cfelse>
			<cfset results.message = getProperty("resourceBundleService").getResourceBundle().getResource("trackbackcreatesuccess") />
		</cfif>
		
		<!--- handle additional form stuff that couldn't be put in the event bean --->
		<!--- if this is a logged-in user, set the created by id --->
		<cfif getTrackbackService().getUserFacade().userExists() 
					AND getTrackbackService().getUserFacade().getUser().getUserId() NEQ "">
			<cfset trackback.getAudit().setCreatedById(getTrackbackService().getUserFacade().getUser().getUserId()) />
		</cfif>
		
		<!--- set audit data values --->
		<cfset trackback.getAudit().setDtCreated(getProperty("resourceBundleService").getLocaleUtils().toEpoch(Now())) />
		<cfset trackback.getAudit().setIpCreated(CGI.REMOTE_ADDR) />
		
		<!--- set active status based on trackback moderation property --->
		<cfif getProperty("moderateTrackbacks")>
			<cfset trackback.getAudit().setIsActive(0) />
		<cfelse>
			<cfset trackback.getAudit().setIsActive(1) />
		</cfif>
		
		<!--- handle mod data for update --->
		<cfif trackback.getTrackbackId() NEQ "">
			<cfset trackback.getAudit().setModifiedById(getTrackbackService().getUserFacade().getUserId()) />
			<cfset trackback.getAudit().setDtModified(getProperty("resourceBundleService").getLocaleUtils().toEpoch(Now())) />
			<cfset trackback.getAudit().setIpModified(CGI.REMOTE_ADDR) />
			
			<cfif arguments.event.isArgDefined("isActive") AND arguments.event.getArg("isActive")>
				<cfset trackback.getAudit().setIsActive(arguments.event.getArg("isActive")) />
			<cfelse>
				<cfset trackback.getAudit().setIsActive(0) />
			</cfif>
		</cfif>
		
		<cfif trackback.getTrackbackId() EQ "">
			<cftry>
				<cfset getTrackbackService().createTrackback(trackback) />
				<cfcatch type="any">
					<cfset results.success = false />
					<cfset results.message = getProperty("resourceBundleService").getResourceBundle().getResource("trackbackcreatefailed") />
				</cfcatch>
			</cftry>
		<cfelse>
			<cftry>
				<cfset getTrackbackService().updateTrackback(trackback) />
				<cfcatch type="any">
					<cfset results.success = false />
					<cfset results.message = getProperty("resourceBundleService").getResourceBundle().getResource("trackbackupdatefailed") />
				</cfcatch>
			</cftry>
		</cfif>
		
		<!--- if the operation was successful, remove the trackback bean and id from the event so it 
				doesn't show again --->
		<cfif results.success>
			<cfset arguments.event.removeArg("trackback") />
			<cfset arguments.event.removeArg("trackbackId") />
		</cfif>
		
		<cfset arguments.event.setArg("message", results.message) />
		<cfset announceEvent("showTrackbacks", arguments.event.getArgs()) />
	</cffunction>
</cfcomponent>